package com.example.springboot26.config;

import com.example.springboot26.filter.MyFilter;
import com.example.springboot26.listener.MyListener;
import com.example.springboot26.servlet.MyServlet;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;


/**
 * 项目名称：springboot26
 * 类 名 称：MyServerConfig
 * 类 描 述：TODO
 * 创建时间：2020/2/6 8:55 下午
 */
@Configuration
public class MyServerConfig {
    // 注册三大组件
    @Bean
    public ServletRegistrationBean myServlet() {
        ServletRegistrationBean srb = new ServletRegistrationBean(new MyServlet(), "/myServlet");
        srb.setLoadOnStartup(1);
        return srb;
    }

    // 过滤器
    @Bean
    public FilterRegistrationBean myFilter() {
        FilterRegistrationBean fr = new FilterRegistrationBean();
        fr.setFilter(new MyFilter());
        fr.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
        return fr;
    }

    // 监听器
    @Bean
    public ServletListenerRegistrationBean myListener() {
        return new ServletListenerRegistrationBean<>(new MyListener());
    }

    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            //定制嵌入式的Servlet容器相关的规则
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(8083);
            }
        };
    }
}
